package cbh.tool.file;

import cbh.tool.convert.MapBean;
import cbh.tool.convert.MapClass;
import cbh.tool.convert.MapStringList;
import cbh.tool.mybatis.annotation.IgnoreColumn;
import cbh.tool.mybatis.annotation.MyColumnName;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hx-pc on 16-1-23.
 */
public class MybatisXmlBuilder extends AbstractDocumentBuilder {

    private String className;

    private String namespace;

    private String tableName;

    private List<MapBean> collections;

    private String idName;

    private String typeName;

    private MapStringList inserts;

    private MapStringList updates;


    private void reset() {
        className = getC().getSimpleName();
        namespace = getPropertits().nameSpace();
        tableName = getTable().tableName();
        idName = getPropertits().idName();
        typeName = getPropertits().typeName();
        Map map = new HashMap<>();
        Map idMap = new HashMap<>();
        Field[] fields = getFields();
        for (int i = 0 ; i < fields.length ; i++) {
            IgnoreColumn ignoreColumn = fields[i].getAnnotation(IgnoreColumn.class);
            if(ignoreColumn == null) {
                MyColumnName columnName = fields[i].getAnnotation(MyColumnName.class);
                if(columnName != null){
                    if(columnName.isPrimary()) {
                        idMap.put(columnName.columnName(),fields[i].getName());
                    }
                    map.put(columnName.columnName(),fields[i].getName());
                }else {
                    map.put(fields[i].getName(),fields[i].getName());
                }
            }
        }
        collections = MapClass.toClass(map);
        inserts = MapClass.toInsertStringList(map);
        updates = MapClass.toUpdateClass(idMap,map);
    }

    @Override
    public void create() {
        reset();
        STGroup stGroup = new STGroupFile("mybatis.xml.stg");
        ST st = stGroup.getInstanceOf("mapper");
        st.add("namespace",namespace);
        st.add("tableName",tableName);
        st.add("collections", collections);
        st.add("idName",idName);
        st.add("typeName",typeName);
        st.add("inserts",inserts);
        st.add("updates",updates);
        String xmlTxt = st.render();
        File file = new File(getBasePath()+"/xml/"+className+".batis.xml");
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            file.createNewFile();
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos);
            osw.write(xmlTxt);
            fos.flush();
            osw.flush();
            fos.close();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
